package com.education.welco.controller.app;

import com.education.common.pojo.AjaxResult;
import com.education.common.utils.ApiReturnObject;
import com.education.welco.pojo.*;
import com.education.welco.pojo.vo.ResourceEntityVo;
import com.education.welco.pojo.vo.SCatalogMenuVo;
import com.education.welco.pojo.vo.VirtualRealityVO;
import com.education.welco.service.*;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/courseRes")
@Api(tags = {"App课程介绍+课程目录资源"})
public class AppCourseDirectoryResController {

    @Autowired
    private ISCatalogWorkService catalogWorkService;
    @Autowired
    private ISAnsweringService answeringService;
    @Autowired
    private ISResourcesService resourcesService;
    @Autowired
    private ISCatalogMenuService catalogMenuService;
    //SCatalog  实体类 属性不够补充   两张表可以看情况结合
    @Autowired
    private ISCatalogService catalogService;
    @Autowired
    private ISFlashClassService sFlashClassService;
    @Autowired
    private IVirtualRealityService virtualRealityService;

    /**
     * 根据课程id查询  授课老师+课程信息+课程目标+课程公告   s_theory_classroom(排课表)+s_catalog_menu(备课资源表)
     *
     * @param courseId 课程id(第一级别)
     * @return
     */
    @GetMapping("/courseIntro")
    @ApiOperation(value = "课程介绍", httpMethod = "GET")
    public AjaxResult courseIntro(@RequestParam(value = "courseId", required = true) String courseId) {
        SCatalog CatalogInfo = catalogService.courseIntro(courseId);
        return AjaxResult.success(CatalogInfo);
    }

    // app端的课程目录结构（一二级）  lessonId-备课资源课程ID
    // /api/welco/sCatalogMenu/listOthers?catalogId=681    查询目录级别
    @GetMapping("/lessonCatalog")
    @ApiOperation(value = "课程目录查询", httpMethod = "GET")
    public ApiReturnObject lessonCatalog(@RequestParam(value = "lessonId", required = true) Integer lessonId) {
        //根据课程id 查询出 之下的目录
        if (lessonId != null) {
            SCatalogMenu sCatalogMenu = new SCatalogMenu();
            //查询出第1级列表
            sCatalogMenu.setParentId(0);
            sCatalogMenu.setId(lessonId);
            SCatalogMenuVo list = catalogMenuService.selectSCatalogMenuList22(sCatalogMenu);
            //2级 以及 下面子列表
            SCatalogMenuVo sCatalogMenus = listSon2(list);
            if (sCatalogMenus.getChildren() == null) {
                return new ApiReturnObject("202", "暂无教学大纲，请先编辑教学大纲", "");
            }
            return new ApiReturnObject("200", "查询成功", sCatalogMenus);
        } else {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
    }

    //查询目录列表
    public SCatalogMenuVo listSon2(SCatalogMenuVo cList) {
        //根据课程id查询出一级目录
        List<SCatalogMenuVo> jrCatalogs = catalogMenuService.selectSCatalogMenuByParentId1(cList.getId());
        if (jrCatalogs != null && jrCatalogs.size() > 0) {
            //设置子级集合
            cList.setChildren(listSon(jrCatalogs));
        }
        return cList;
    }

    //查询目录列表
    public List<SCatalogMenuVo> listSon(List<SCatalogMenuVo> cList) {
        for (SCatalogMenuVo catalogMenu : cList) {
            //根据第一级的id查询出二级列表
            List<SCatalogMenuVo> jrCatalogs = catalogMenuService.selectSCatalogMenuByParentId1(catalogMenu.getId());
            if (jrCatalogs != null && jrCatalogs.size() > 0) {
                //设置子级集合
                catalogMenu.setChildren(listSon(jrCatalogs));
            }
        }
        return cList;
    }

    /**
     * 根据课程id以及学生班级id查询课程二级目录下的资源信息
     *
     * @param lessonId 课程二级目录id
     * @param classId  班级id
     * @param type     资源类型type-教学课件1、FLASH微课7、虚拟教学8、录播视频9、实景视频5、实战照片4、课前导学2、课堂答疑3、课后作业6、课堂考试10
     * @return resourceMap
     * lessonId=753&classId=119
     */
    @GetMapping("/lessonResource")
    @ApiOperation(value = "课程目录下二级资源", httpMethod = "GET")
    public AjaxResult lessonResource(@RequestParam(value = "lessonId", required = true) Integer lessonId,
                                     @RequestParam(value = "classId", required = false) Integer classId,
                                     @RequestParam(value = "type", required = true) String type) {
        Map<String, Object> resourceMap = Maps.newHashMap();
        //教学课件 type-1
        if ("1".equals(type)) {
            SResources courseware = new SResources();
            courseware.setRCatalogid(lessonId);
            courseware.setType("1");
            List<SResources> coursewareList = resourcesService.selectSResourcesList(courseware);

            List<ResourceEntityVo> resourceEntityVos = new ArrayList<>();
            if (coursewareList.size() > 0) {
                for (SResources sResources : coursewareList) {
                    ResourceEntityVo resourceEntityVo = new ResourceEntityVo();
                    resourceEntityVo.setId(sResources.getId());
                    resourceEntityVo.setName(sResources.getRName());
                    resourceEntityVo.setUrl(sResources.getRUrl());
                    resourceEntityVos.add(resourceEntityVo);
                }
            }
            resourceMap.put("list", resourceEntityVos);
        } else if ("7".equals(type)) {
            //FLASH微课  type-7
            SResources microClass = new SResources();
            microClass.setRCatalogid(lessonId);
            microClass.setType("7");
            List<SResources> microClassList = resourcesService.selectSResourcesList(microClass);

            List<ResourceEntityVo> resourceEntityVos = new ArrayList<>();
            if (microClassList.size() > 0) {
                for (SResources sResources : microClassList) {
                    ResourceEntityVo resourceEntityVo = new ResourceEntityVo();
                    resourceEntityVo.setId(sResources.getId());
                    resourceEntityVo.setName(sResources.getRName());
                    resourceEntityVo.setUrl(sResources.getRUrl());
                    resourceEntityVos.add(resourceEntityVo);
                }
            }
            resourceMap.put("list", resourceEntityVos);
        } else if ("8".equals(type)) {
            //虚拟教学  待定
            //SFlashClass sFlashClass = new SFlashClass();
            //List<SFlashClass> list = sFlashClassService.selectSFlashClassList(sFlashClass);
            //List<SFlashClass> list = sFlashClassService.selectSFlashClassList1(sFlashClass);

            List<VirtualRealityVO> virtualRealityVOS = selectCatalogShareVRList(lessonId);
            List<ResourceEntityVo> resourceEntityVos = new ArrayList<>();
            if (virtualRealityVOS.size() > 0) {
                for (VirtualRealityVO virtualRealityVO : virtualRealityVOS) {
                    ResourceEntityVo resourceEntityVo = new ResourceEntityVo();
                    resourceEntityVo.setId(virtualRealityVO.getId());
                    resourceEntityVo.setName(virtualRealityVO.getName());
                    resourceEntityVo.setUrl(virtualRealityVO.getKeyword());
                    resourceEntityVo.setType(String.valueOf(virtualRealityVO.getValidState()));
                    resourceEntityVos.add(resourceEntityVo);
                }
            }
            resourceMap.put("list", resourceEntityVos);
        } else if ("9".equals(type)) {
            //录播视频  待定Video recording
            resourceMap.put("videoRecord", new ArrayList<>());
        } else if ("5".equals(type)) {
            //实景视频  type-5
            SResources realPracticeVideo = new SResources();
            realPracticeVideo.setRCatalogid(lessonId);
            realPracticeVideo.setType("5");
            List<SResources> realPracticeVideoList = resourcesService.selectSResourcesList(realPracticeVideo);

            List<ResourceEntityVo> resourceEntityVos = new ArrayList<>();
            if (realPracticeVideoList.size() > 0) {
                for (SResources sResources : realPracticeVideoList) {
                    ResourceEntityVo resourceEntityVo = new ResourceEntityVo();
                    resourceEntityVo.setId(sResources.getId());
                    resourceEntityVo.setName(sResources.getRName());
                    resourceEntityVo.setUrl(sResources.getRUrl());
                    resourceEntityVos.add(resourceEntityVo);
                }
            }
            resourceMap.put("list", resourceEntityVos);
        } else if ("4".equals(type)) {
            //实战照片  type-4
            SResources realPracticeImage = new SResources();
            realPracticeImage.setRCatalogid(lessonId);
            realPracticeImage.setType("4");

            List<SResources> realPracticeImageList = resourcesService.selectSResourcesList(realPracticeImage);

            List<ResourceEntityVo> resourceEntityVos = new ArrayList<>();
            if (realPracticeImageList.size() > 0) {
                for (SResources sResources : realPracticeImageList) {
                    ResourceEntityVo resourceEntityVo = new ResourceEntityVo();
                    resourceEntityVo.setId(sResources.getId());
                    resourceEntityVo.setName(sResources.getRName());
                    resourceEntityVo.setUrl(sResources.getRUrl());
                    resourceEntityVos.add(resourceEntityVo);
                }
            }
            resourceMap.put("list", resourceEntityVos);
        } else if ("2".equals(type)) {
            //课前导学  type-2
            SResources preClassLead = new SResources();
            preClassLead.setRCatalogid(lessonId);
            preClassLead.setType("2");

            List<SResources> preClassLeadList = resourcesService.selectSResourcesList(preClassLead);

            List<ResourceEntityVo> resourceEntityVos = new ArrayList<>();
            if (preClassLeadList.size() > 0) {
                for (SResources sResources : preClassLeadList) {
                    ResourceEntityVo resourceEntityVo = new ResourceEntityVo();
                    resourceEntityVo.setId(sResources.getId());
                    resourceEntityVo.setName(sResources.getRName());
                    resourceEntityVo.setUrl(sResources.getRUrl());
                    resourceEntityVos.add(resourceEntityVo);
                }
            }
            resourceMap.put("list", resourceEntityVos);
        } else if ("3".equals(type)) {
            //课堂答疑   s_answering   s_queans_class
            SAnswering sAnswering = new SAnswering();
            sAnswering.setCatalogId(lessonId);
            List<SAnswering> sAnsweringList = answeringService.selectSAnsweringStuList(sAnswering);
            resourceMap.put("list", sAnsweringList);
        } else if ("6".equals(type)) {
            //课后作业 s_catalog_work   s_homeword_class
            SCatalogWork sCatalogWork = new SCatalogWork();
            sCatalogWork.setCatalogId(lessonId);
            List<SCatalogWork> sCatalogWorkList = catalogWorkService.selectCatalogWorkStuList(sCatalogWork);
            resourceMap.put("list", sCatalogWorkList);
        } else if ("10".equals(type)) {
            //课堂考试
            resourceMap.put("list", new ArrayList<>());
        }
        return AjaxResult.success(resourceMap);
    }

    public List<VirtualRealityVO> selectCatalogShareVRList(Integer lessonId) {
        Integer school = virtualRealityService.selectSchoolIdBycatalogId(lessonId);
        return virtualRealityService.selectCatalogShareVRList(school, lessonId);
    }

}
